import {defHttp} from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";
import { ExportParams } from '/@/views/dw/types'; // 可自定义参数类型

const { createConfirm } = useMessage();

enum Api {
  list = '/dw/dwProPlan/list',
  save='/dw/dwProPlan/add',
  edit='/dw/dwProPlan/edit',
  deleteOne = '/dw/dwProPlan/delete',
  deleteBatch = '/dw/dwProPlan/deleteBatch',
  importExcel = '/dw/dwProPlan/importExcel',
  exportXls = '/dw/dwProPlan/exportXls',
  childlist = '/dw/dwAssemble/list',
  deleteOneChild = '/dw/dwAssemble/delete',
  onEx='/dw/dwProPlan/calculateDataexportXls',
}

// 新增：弹窗导出专用方法（直接返回Blob流，用于下载）
export const exportPlanReport = async (params: ExportParams) => {
  return defHttp.get({
    url: Api.onEx,
    params,
    responseType: 'blob' ,  // 关键：设置响应类型为Blob（文件流）
    timeout: 60000, // 延长超时（导出Excel可能耗时）
    transformResponse: [data => data] ,  // 关键：覆盖默认的响应转换，避免Blob被转为JSON
    headers: {
      'Accept': 'application/octet-stream, application/vnd.ms-excel'
    }
  });



};
// export const exportPlanReport = async (params: { monTime: string }) => {
//   return defHttp.get({
//     url: Api.onEx,
//     params,
//     responseType: 'blob',
//     timeout: 60000,
//     transformResponse: [data => data],
//     headers: {
//       'Accept': 'application/octet-stream, application/vnd.ms-excel'
//     }
//   });


/**
 * 导出api
 * @param params
 */
export const getExportUrl = (isOnEx: boolean) => {
  let  url  = isOnEx ? Api.onEx : Api.exportXls;
  console.log(">>> 路径导出api >>" +url);

  return url;
}
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
  defHttp.get({url: Api.list, params});
/**
 * 子列表接口
 * @param params
 */
export const childlist = (params) =>
  defHttp.get({url: Api.childlist, params});

/**
 * 删除单个
 */
export const deleteOne = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOne, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
/**
 *  子 删除单个
 */
export const deleteOneChild = (params,handleSuccess) => {
  return defHttp.delete({url: Api.deleteOneChild, params}, {joinParamsToUrl: true}).then(() => {
    handleSuccess();
  });
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
  createConfirm({
    iconType: 'warning',
    title: '确认删除',
    content: '是否删除选中数据',
    okText: '确认',
    cancelText: '取消',
    onOk: () => {
      return defHttp.delete({url: Api.deleteBatch, data: params}, {joinParamsToUrl: true}).then(() => {
        handleSuccess();
      });
    }
  });
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate) => {
  let url = isUpdate ? Api.edit : Api.save;
  return defHttp.post({url: url, params});
}

export const getOnEx = (params: any, config?: any) => {
  return defHttp.get({url: Api.onEx, params,...config});
}

